package com.sicpa.common.utils;

import com.sicpa.common.config.FtpConfig;
import com.sicpa.common.utils.spring.SpringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.MalformedURLException;

/**
 * FtpFileHandelUtils
 *
 * @author lwf
 */
public class FtpFileHandelUtils {

    private static final Logger log = LoggerFactory.getLogger(FtpFileHandelUtils.class);

    public FTPClient ftpClient = null;

    /**
     * 初始化ftp服务器
     */
    public void initFtpClient() {
        ftpClient = new FTPClient();
        ftpClient.setControlEncoding("utf-8");
        FtpConfig ftpConfig = SpringUtils.getBean(FtpConfig.class);
        try {
            System.out.println("connecting...ftp服务器:" + ftpConfig.getHostname() + ":" + ftpConfig.getPort());
            //连接ftp服务器
            ftpClient.connect(ftpConfig.getHostname(), ftpConfig.getPort());
            //登录ftp服务器
            ftpClient.login(ftpConfig.getUsername(), ftpConfig.getPwd());
            //是否成功登录服务器
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                System.out.println("connect failed...ftp服务器:" + ftpConfig.getHostname() + ":" + ftpConfig.getPort());
            }
            System.out.println("connect successfu...ftp服务器:" + ftpConfig.getHostname() + ":" + ftpConfig.getPort());
        } catch (MalformedURLException e) {
            log.error(e.getMessage(), e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

}
